import json

from testapp.excute.commKeyword import CommKeyword
from testapp.dataio.dataiofactory import DataIOFactory

class MethodFactory(object):
    fac = DataIOFactory()
    def __init__(self):
        self.comKey = CommKeyword()
        # self.comKW = dict(myCof.items('commkey'))
        isOK, result = self.fac.getCommonMethod();
        if isOK:
            self.comKW = result
        else:
            return isOK, result




    def method_factory(self, **kwargs):
        """
        用例公共方法工厂
        默认传的参数为：
        result:用来接收结果的变量，格式可为${abc}
        method:调用的方法，这里设计方法都使用中文
        param_x:参数，数量不限。格式可为${abc}会替换为已存在的数据
        """

        if kwargs.__len__() > 0:
            try:
                kwargs['method']
            except KeyError:
                try:
                    kwargs['apimethod']
                except KeyError:
                    return False, 'keyword:用例[method]字段方法没参数为空.'
            try:
                method = self.comKW[str(kwargs['method'])]
            except KeyError:
                try:
                    method = self.comKW[str(kwargs['apimethod']).lower()]
                except KeyError:
                    return False, 'keyword:方法[' + kwargs['method'] + '] 不存在,或未注册.'
        try:
            func = getattr(self.comKey, method, None)
            _isOk, reselt = func(**kwargs)
            return _isOk, reselt
        except Exception as e:
            return False, '执行失败，具休原因：[{}]'.format(e)





# fac = MethodFactory()
# print(fac.method_factory(method='获取当前用例文件名称'))

# fac = DataIOFactory()
# result = fac.getCommonMethod();
# a = result['JSON串取值']
# print(a)
